package net.jeeshop.core.filter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.jeeshop.core.ManageContainer;
public class LoginFilter implements Filter {
    SessionCountService sessionCountService;
    public void init(FilterConfig arg0) throws ServletException {
        WebApplicationContext app =
                WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
        sessionCountService = (SessionCountService) app.getBean("sessionCountService");
    }
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        if(true){
            chain.doFilter(request, response);
            return ;
        }
        HttpServletRequest req = (HttpServletRequest) request;
        System.out.println(req.getServletPath());
        System.out.println(req.getSession().isNew());
        if(req.getSession().isNew()){
            SessionCount sessionCount = new SessionCount();
            sessionCount.setIp(getIp(req));
            sessionCount.setStarttime()
            sessionCountService.insert(sessionCount);
        }
        if(req.getServletPath().startsWith("/index.jsp") &&
                req.getSession().getAttribute("fromActionOut")==null){
            HttpServletResponse res = (HttpServletResponse) response;
            String url = req.getRequestURL().toString();
            String f = url.substring(0, url.indexOf(req.getContextPath()));
            String p = f + req.getContextPath() + "/index!q.action?id=-1";
            System.out.println("sendRedirect...");
            res.sendRedirect(p);
            return;
        }
        req.getSession().setAttribute("fromActionOut",null);
        if (req.getSession().getAttribute(ManageContainer.manage_session_user_info) == null){
            if (req.getServletPath().startsWith("/manage")
                    && !req.getServletPath().startsWith("/manage/admin.jsp")
                    && !canPass(req.getServletPath())) {
                HttpServletResponse res = (HttpServletResponse) response;
                String url = req.getRequestURL().toString();
                String f = url.substring(0, url.indexOf(req.getContextPath()));
                String p = f + req.getContextPath() + "/manage/admin.jsp";
                System.out.println("sendRedirect...");
                res.sendRedirect(p);
                return;
            }
        }else{
            if(req.getServletPath().equals("/manage") || req.getServletPath().equals("/manage/")){
                HttpServletResponse res = (HttpServletResponse) response;
                String url = req.getRequestURL().toString();
                String f = url.substring(0, url.indexOf(req.getContextPath()));
                String p = f + req.getContextPath() + "/manage/user!login.action";
                System.out.println("sendRedirect...");
                res.sendRedirect(p);
                return;
            }
        }
        chain.doFilter(request, response);
    }
    private String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
    static Map<String, String> canMap = new HashMap<String, String>();
    static Map<String, String> menuMap = new HashMap<String, String>();
    static {
        canMap.put("indexJsp1", "/index.jsp");
        canMap.put("admin", "/admin.jsp");
        canMap.put("indexJsp", "login/index.jsp");
        canMap.put("indexAction", "user!login.action");
        canMap.put("crawl/barcode!query.action", "barcode!query.action");
        canMap.put("barcode.do", "barcode.do");
        canMap.put("/manage/news!toIndex.action", "/manage/news!toIndex.action");
        canMap.put("/manage/account!ajaxLogin.action", "/manage/account!ajaxLogin.action");
        canMap.put("manage/account!loginOut.action", "manage/account!loginOut.action");
        canMap.put("/manage/account.action", "/manage/account.action");
        canMap.put("/regeist/manage/account!ajaxLogin.action", "/regeist/manage/account!ajaxLogin.action");
        menuMap.put("/toIndex.jsp", "");
        menuMap.put("/manage/news!toIndex.action", "");
        menuMap.put("/search!p.action", "");
        menuMap.put("/search!g.action", "");
        menuMap.put("/publish!publish.action", "");
        menuMap.put("/download!download.action", "");
        menuMap.put("/barcode!barcode.action", "");
        menuMap.put("/about!about.action", "");
    }
    private boolean canPass(String servletPath) {
        for (Iterator<Entry<String, String>> it = canMap.entrySet().iterator(); it
                .hasNext();) {
            Entry<String, String> entry = it.next();
            if (servletPath.indexOf(entry.getValue()) != -1) {
                return true;
            }
        }
        return false;
    }
    public void destroy() {
    }
}
package net.jeeshop.core.freemarker.front;
        import java.io.BufferedWriter;
        import java.io.File;
        import java.io.FileOutputStream;
        import java.io.IOException;
        import java.io.OutputStreamWriter;
        import java.io.Writer;
        import java.util.HashMap;
        import java.util.List;
        import java.util.Locale;
        import java.util.Map;
        import javax.servlet.ServletContext;
        import net.jeeshop.services.front.news.NewsService;
        import net.jeeshop.services.front.news.bean.News;
        import net.jeeshop.services.front.product.ProductService;
        import net.jeeshop.services.front.product.bean.Product;
        import org.apache.commons.lang.StringUtils;
        import org.apache.log4j.Logger;
        import org.apache.struts2.ServletActionContext;
        import freemarker.template.Configuration;
        import freemarker.template.Template;
        import freemarker.template.TemplateException;
public class FreemarkerHelper {
    private static final Logger logger = Logger.getLogger(FreemarkerHelper.class);
    private ProductService productService;
    private NewsService newsService;
    public static final String template_newsInfo = "newsInfo.ftl";
    public static final String template_product = "product.ftl";
    public void setProductService(ProductService productService) {
        this.productService = productService;
    }
    public void setNewsService(NewsService newsService) {
        this.newsService = newsService;
    }
    public void index(String template, String templateHtml) {
        try {
            HashMap<String, Object> data = new HashMap<String, Object>();
            data.put("indexMenuList", loadIndexMenu());
            crateHTML(ServletActionContext.getServletContext(), data, template,
                    templateHtml);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }private String intercept0(ActionInvocation actionInvocation) throws Exception {
        String actionName = actionInvocation.getProxy().getActionName();
        Object action = actionInvocation.getProxy().getAction();
        String method = actionInvocation.getProxy().getMethod();
        String namespace = actionInvocation.getProxy().getNamespace();
        Object action2 = actionInvocation.getAction();
        if(StringUtils.isBlank(method)){
            throw new NullPointerException;
        }		logger.error(">>ManageInterceptor==actionName="+actionName+";method="+method+";action="+action+";namespace="+namespace);
        logger.error(">>ManageInterceptor==actionName="+actionName+";method="+method+";action="+action+";namespace="+namespace+";action2="+action2);
        String url = actionName+"!"+method+".action";
        StringBuffer urlBuff = new StringBuffer(actionName);
        urlBuff.append("!").append(method).append(".action");
        actionInvocation.getInvocationContext().
        if (action2 instanceof UserAction || action2 instanceof MenuAction) {
            return actionInvocation.invoke();
        }
        private List<IndexMenu> loadIndexMenu() {
            IndexMenu e = new IndexMenu();
            List<IndexMenu> indexMenuList = indexMenuService.selectList(e);
            return indexMenuList;
        }
    public void crateHTML(ServletContext context, Map<String, Object> data,
                          String templatePath, String targetHtmlPath) throws Exception {
        if(StringUtils.isBlank(targetHtmlPath)){
            throw new NullPointerException;
        }
        Configuration freemarkerCfg = new Configuration();
        freemarkerCfg.setServletContextForTemplateLoading(context, "/template");
        freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8");
        freemarkerCfg.setDefaultEncoding("UTF-8");
        Template template = freemarkerCfg
                .getTemplate(templatePath, "UTF-8");
        template.setEncoding("UTF-8");
        String htmlPath = context.getRealPath("/") + "/"
                + targetHtmlPath;
        System.out.println(targetHtmlPath);
        File htmlFile = new File(targetHtmlPath);
        Writer out = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(htmlFile), "UTF-8"));
        template.process(data, out);
        out.flush();
        out.close();
        System.out.println;
        try {
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println;
        }
    }
    public boolean isEmpty(String value) {
        if (value == null || value.trim().length() == 0) {
            return true;
        }
        return false;
    }
    public void helps() throws Exception {
        News param = new News();
        param.setType("help");
        List<News> notices = newsService.selectList(param);
        if(notices==null || notices.size()==0){
            logger.error("notices size = 0");
            return;
        }
        logger.error("notices size = " + notices.size());
        HashMap<String, Object> data = new HashMap<String, Object>();
        for(int i=0;i<notices.size();i++){
            News news = notices.get(i);
            if(StringUtils.isBlank(news.getContent())){
                continue;
            }
            data.clear();
            data.put("e", news);
            String templateHtml =
                    ServletActionContext.getServletContext().getRealPath("/")+"/jsp/helps/"+news.getId()+".jsp";
            crateHTML(ServletActionContext.getServletContext(), data,
                    template_newsInfo,templateHtml);
        }
    }
    public void notices() throws Exception {
        News param = new News();
        param.setType("notice");
        List<News> notices = newsService.selectList(param);
        if(notices==null || notices.size()==0){
            logger.error("notices size = 0");
            return;
        }
        logger.error("notices size = " + notices.size());
        HashMap<String, Object> data = new HashMap<String, Object>();
        for(int i=0;i<notices.size();i++){
            News news = notices.get(i);
            if(StringUtils.isBlank(news.getContent())){
                continue;
            }
            data.clear();
            data.put("e", news);
            String templateHtml =
                    ServletActionContext.getServletContext().getRealPath("/")+"/jsp/notices/"+news.getId()+".jsp";
            crateHTML(ServletActionContext.getServletContext(), data,
                    template_newsInfo,templateHtml);
        }
    }
    public String products() throws Exception {
        List<Product> productList = productService.selectListProductHTML(new Product());
        if(productList==null || productList.size()==0){
            logger.error("productList size = 0");
            return null;
        }
        logger.error("productList size = " + productList.size());
        StringBuilder errorBuff = new StringBuilder();
        HashMap<String, Object> data = new HashMap<String, Object>();
        for(int i=0;i<productList.size();i++){
            Product p = productList.get(i);
            if(StringUtils.isBlank(p.getProductHTML())){
                continue;
            }
            data.clear();
            data.put("e", p);
            String templateHtml =
                    ServletActionContext.getServletContext().getRealPath("/")+"/jsp/product/"+p.getId()+".jsp";
            try {
                crateHTML(ServletActionContext.getServletContext(), data,
                        template_product,templateHtml);
            } catch (Exception e) {
                e.printStackTrace();
                errorBuff.append(p.getId()+",");
                continue;
            }
        }
        if(errorBuff.length()==0){
            return null;
        }
        return errorBuff.toString();
    }
    public String staticProductByID(String id) throws Exception {
        Product p = productService.selectById(id);
        if(p==null || StringUtils.isBlank(p.getProductHTML())){
            logger.error("ERROR,not found product by id = " + id);
            throw new NullPointerException("ERROR,not found product by id = " + id);
        }
        HashMap<String, Object> data = new HashMap<String, Object>();
        data.clear();
        data.put("e", p);
        String templateHtml =
                ServletActionContext.getServletContext().getRealPath("/")+"/jsp/product/"+p.getId()+".jsp";
        crateHTML(ServletActionContext.getServletContext(), data, template_product,templateHtml);
        return "success";
    }
    public String staticNewsByID(String id) throws Exception {
        if(StringUtils.isBlank(id)){
        }
        News news = newsService.selectById(id);
        if(news==null || StringUtils.isBlank(news.getContent())){
            logger.error("ERROR,not found news by id = " + id);
            throw new NullPointerException("ERROR,not found news by id = " + id);
        }
        HashMap<String, Object> data = new HashMap<String, Object>();
        data.clear();
        data.put("e", news);
        String templateHtml = null;
        if(news.getType().equals(News.news_type_help)){
            templateHtml =
                    ServletActionContext.getServletContext().getRealPath("/")+"/jsp/helps/"+news.getId()+".jsp";
        }else if(news.getType().equals(News.news_type_notice)){
            templateHtml =
                    ServletActionContext.getServletContext().getRealPath("/")+"/jsp/notices/"+news.getId()+".jsp";
        }
        crateHTML(ServletActionContext.getServletContext(), data, template_newsInfo,templateHtml);
        return "success";
    }
}